package com.suray.system.run.data.mysql.device.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.system.run.data.mysql.device.entity.ExecTaskDB;
import com.suray.system.run.util.ExecTaskTime;
import com.suray.wcs.service.enums.TaskType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ExecTaskDBServiceImpl extends ServiceImpl<ExecTaskDBMapper, ExecTaskDB> implements ExecTaskDBService {

    @Resource
    private ExecTaskDBMapper execTaskDBMapper;

    @Override
    public ExecTaskDB findExecTask(String series) {
        LambdaQueryWrapper<ExecTaskDB> eq = new QueryWrapper<ExecTaskDB>().lambda()
                .eq(ExecTaskDB::getSeries, series).isNull(ExecTaskDB::getEndTime);

        List<ExecTaskDB> execTaskDBS = this.list(eq);
        if (execTaskDBS.size() == 0) {
            return null;
        }
        return execTaskDBS.get(0);
    }

    public int getDeviceTaskNum(String series, TaskType taskType, Date startTime, Date endTime) {
        Integer type = taskType == null ? null : taskType.getTypeNum();
        Integer num = execTaskDBMapper.calcTaskNumByTaskType(series, startTime, endTime, type);
        return num == null ? 0 : num;
    }
}
